package com.superbytecode.cloud.web.swagger;

import com.superbytecode.cloud.common.utils.VerifyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import springfox.documentation.builders.PropertySpecificationBuilder;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;

import java.lang.reflect.Field;

/**
 * All rights Reserved, Designed By www.super-master.com
 *
 * @ProjectName: beacon-cloud
 * @Package: com.superbytecode.cloud.web.swagger
 * @ClassName: SwaggerUtil
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2024/10/22 12:03
 * @Version: V1.0
 * @Copyright: 2024 www.super-master.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Slf4j
public class SwaggerUtil {
    private SwaggerUtil() {
    }

    /**
     * 追加swagger描述
     *
     * @param context context
     * @param message 消息
     */
    public static void append(ModelPropertyContext context, String message) {
        if (StringUtils.isBlank(message)) {
            return;
        }
        PropertySpecificationBuilder specificationBuilder = context.getSpecificationBuilder();
        try {
            Field field = specificationBuilder.getClass().getDeclaredField("description");
            field.setAccessible(true);
            Object description = field.get(specificationBuilder);
            if (VerifyUtil.nonNull(description)) {
                specificationBuilder.description(description + message);
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            log.error("swagger描述信息追加失败", e);
        }
    }
}